package gcode.helloworld.java.cmd;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CmdLine {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GnuParser parser=new GnuParser();
		Options opts=getCmdOptions();
		CommandLine line=null;
		try {
			line=parser.parse(opts,args);
		} catch (ParseException e) {
			//e.printStackTrace();
			System.err.println(e.getMessage());
			HelpFormatter helper=new HelpFormatter();
			helper.printHelp("cmdLineSyntax", opts);
		}
		if(line==null) return ;
		if(line.hasOption("h")){
			HelpFormatter helper=new HelpFormatter();
			//helper.printHelp("cmdLineSyntax", opts);
			helper.printHelp(80, "tester", "header", opts, "footer");
			return;
		}
		if(line.hasOption("debug")){
			System.out.println("debug is set");
		}
		if(line.hasOption("arg")){
			String arg=line.getOptionValue("arg");
			System.out.println("arg "+arg);
		}
	}

	private static Options getCmdOptions() {
		Options options=new Options();
		Option opt=OptionBuilder.create("debug");
		options.addOption(opt);
		OptionBuilder.isRequired();
		OptionBuilder.hasArg();
		OptionBuilder.withArgName("arg");
		OptionBuilder.withDescription("arg description");
		OptionBuilder.withLongOpt("argument");
		OptionBuilder.withValueSeparator();
		opt=OptionBuilder.create("arg");
		options.addOption(opt);
		options.addOption(new Option("h","help",false,"print help"));
		return options;
	}


}
